package com.c.ctgproject.ui.sell.parts

import android.widget.EditText
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.PartSellItemEntityTO
import com.c.ctgproject.entity.PartSellListEntityTO
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.SellPartWidget
import kotlinx.android.synthetic.main.activity_parts_sell2.*
import kotlinx.android.synthetic.main.activity_parts_sell2.mTitleBar
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *作者:LiBW
 *创建日期: 2020/9/9
 *描述: 配件销售页面
 */
class PartsSellActivity : BaseActivity() {
    private val mViewModel: PartsSellViewModel by lazy { ViewModelProvider(this)[PartsSellViewModel::class.java] }
    private val mList = ArrayList<PartSellItemEntityTO>()
    private val carCode: String by lazy { intent.getStringExtra(MyConstants.CAR_CODE).handlerNull() }
    private val carId: String by lazy { intent.getStringExtra(MyConstants.CAR_ID).handlerNull() }


    override fun getLayout() = R.layout.activity_parts_sell2

    override fun event() {
        changeTransparentStatusBar()
        changeStatusIconColor(true)
        mTitleBar.setTitle(carCode)
        mTitleBar.setTitleTextSize(14f)
        //提交回调
        mViewModel.submitResult.observe(this, Observer {
            loading.dismiss()
            MyLog.d("提交回调:$it")
            val data = initData(it)
            if (data.status == 0) {
                CustomToast.showToast(this, "提交成功")
                finish()
            } else {
                CustomToast.showToast(this, data.msg.toString())
            }
        })

        mViewModel.error.observe(this, Observer {
            MyLog.d("失败回调:$it")
            loading.dismiss()
            CustomToast.showToast(this, it)
        })
    }

    private fun handlerData() {
        mList.clear()
        val buyer = mBuyerET.text.toString()
        val buyerPhone = mPhoneNumET.text.toString()
        when {
            buyer.isEmpty() -> CustomToast.showToast(this, "请输入采购方")
            buyerPhone.isEmpty() -> CustomToast.showToast(this, "请输入采购方电话")
            else -> {
                val count = mContainer.childCount
                for (i in 0 until count) {
                    val view = mContainer.getChildAt(i) as SellPartWidget
                    val data = PartSellItemEntityTO("", "", "", "")
                    data.partsName = view.findViewById<EditText>(R.id.mPartNameET).text.toString()
                    data.partsNumber = view.findViewById<EditText>(R.id.mPartsNumberET).text.toString()
                    data.partsPrice = view.findViewById<EditText>(R.id.mPartPriceET).text.toString()
                    data.partsWeight = view.findViewById<EditText>(R.id.mPartsWeightET).text.toString()
                    mList.add(data)
                }
            }
        }
        var flag = false
        MyLog.d("数据量:${mList.size}")
        mList.forEach {
            when {
                it.partsName.isEmpty() -> {
                    flag = false
                    CustomToast.showToast(this, "请将信息填写完整")
                }
                it.partsNumber.isEmpty() -> {
                    CustomToast.showToast(this, "请将信息填写完整")
                    flag = false
                }
                it.partsPrice.isEmpty() -> {
                    CustomToast.showToast(this, "请将信息填写完整")
                    flag = false
                }
//                it.partsWeight.isEmpty() -> {
//                    CustomToast.showToast(this, "请将信息填写完整")
//                    flag = false
//                }
                else -> {
                    flag = true
                }
            }
            MyLog.d("用户填入的数据集合:${it}")
        }
        if (flag) {
            val partSellListEntity = PartSellListEntityTO(carId, buyer, buyerPhone, mList)
            val body = RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
//                getGson().toJson(partSellListEntity)
                getGson().toJson(partSellListEntity)
            )
            loading.show()
            mViewModel.submitParts(body)
        }
    }

    fun refreshItem() {
        val count = mContainer.childCount
        for (i in 0 until count) {
            val view = mContainer.getChildAt(i) as SellPartWidget
            view.findViewById<TextView>(R.id.mDeleteItemTV).setOnClickListener {
                if (count == 1) {
                    CustomToast.showToast(this, "至少添加一件货品")
                } else {
                    mContainer.removeViewAt(i)
                }
                refreshItem()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        mAddPartTV.performClick()
    }

    override fun click() {
        //标题栏添加货品
        mAddPartTV.setOnClickListener {
            mContainer.addView(SellPartWidget(this))
            refreshItem()
        }
        //提交数据
        mConfirmRL.setOnClickListener {
            handlerData()
        }
    }
}